﻿/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

import QtQuick 2.12
import AppUI 1.0 as AppUI
import AppControls2 1.0 as AppControls2
import org.ukui.menu.core 1.0
import org.ukui.quick.items 1.0 as UkuiItems

Item {
    id: root
    clip: true

    property int animationDuration: menuSetting.get("animationDuration")
    property double transparency: mainWindow.transparency
    property bool onComlpeted: false

    Component.onCompleted: {
        mainWindow.fullScreenChanged.connect(enterFullScreen);
        mainWindow.beforeFullScreenExited.connect(exitFullScreen);
        menuSetting.changed.connect(updateAnimationDuration);
        onComlpeted = true;
    }

    function enterFullScreen() {
        if (mainWindow.isFullScreen) {
            normalHide.start();
            fullShow.start();
            enterFullScreenAnimation.start();
            if (root.transparency <= 0.6) {
                maskHide.start();
            }
        }
    }

    function exitFullScreen() {
        normalShow.start();
        fullHide.start();
        exitFullScreenAnimation.start();
        if (root.transparency <= 0.6) {
            maskShow.start();
        }
    }

    function updateAnimationDuration(key) {
        if (key === "animationDuration") {
            root.animationDuration = menuSetting.get("animationDuration");
        }
    }

    onTransparencyChanged: {
        if (transparency > 0.6) {
            backgroundMask.opacity = 1;
        }
    }

    QtObject {
        id: normalScreenGeometry
        property var normalGeometry: mainWindow.normalScreenSize()
        property int normalScreenX: normalScreenMargin
        property int normalScreenY: root.height - normalGeometry.height - normalScreenMargin
        property int normalScreenMargin: 8
        property bool isFullScreen: mainWindow.isFullScreen

        onIsFullScreenChanged: {
            switch (mainWindow.panelPos) {
            case 0 :
                normalScreenX = normalScreenMargin;
                normalScreenY = root.height - normalGeometry.height - normalScreenMargin;
                break;
            case 1 :
                normalScreenX = normalScreenMargin;
                normalScreenY = normalScreenMargin;
                break;
            case 2 :
                normalScreenX = normalScreenMargin;
                normalScreenY = normalScreenMargin;
                break;
            case 3 :
                normalScreenX = root.width - normalGeometry.width - normalScreenMargin;
                normalScreenY = normalScreenMargin;
                break;
            default :
                normalScreenX = normalScreenMargin;
                normalScreenY = root.height - normalGeometry.height - normalScreenMargin;
                break;
            }
        }
    }

    UkuiItems.StyleBackground {
        id: backgroundMask
        // 初始状态默认为normalScreen
        x: 0; y: 0
        height: normalScreenGeometry.normalGeometry.height
        width: normalScreenGeometry.normalGeometry.width
        radius: 12

        onHeightChanged: {
            if (root.transparency <= 0.6 && root.onComlpeted) {
                switch (mainWindow.panelPos) {
                case 0 :
                    mainWindow.changeWindowBlurRegion(0, root.height - height, width, height);
                    break;
                case 1 :
                    mainWindow.changeWindowBlurRegion(0, 0, width, height);
                    break;
                case 2:
                    mainWindow.changeWindowBlurRegion(0, 0, width, height);
                    break;
                case 3 :
                    mainWindow.changeWindowBlurRegion(root.width - width, 0, width, height);
                    break;
                default :
                    mainWindow.changeWindowBlurRegion(0, root.height - height, width, height);
                    break;
                }
            }
        }

        NumberAnimation {
            id: maskHide
            target: backgroundMask; property: "opacity"
            from: 1.0; to: 0;
            duration: root.animationDuration / 4; easing.type: Easing.InOutQuad
        }

        NumberAnimation {
            id: maskShow
            target: backgroundMask; property: "opacity"
            from: 0; to: 1.0;
            duration: root.animationDuration * 2; easing.type: Easing.InOutExpo
        }

        ParallelAnimation {
            id: enterFullScreenAnimation
            PropertyAnimation {
                target: backgroundMask; property: "x"
                from: normalScreenGeometry.normalScreenX; to: 0
                duration: root.animationDuration; easing.type: Easing.InOutQuad
            }
            PropertyAnimation {
                target: backgroundMask; property: "y"
                from: normalScreenGeometry.normalScreenY; to: 0
                duration: root.animationDuration; easing.type: Easing.InOutQuad
            }
            PropertyAnimation {
                target: backgroundMask; property: "height"
                from: normalScreenGeometry.normalGeometry.height; to: root.height
                duration: root.animationDuration; easing.type: Easing.InOutQuad
            }
            PropertyAnimation {
                target: backgroundMask; property: "width"
                from: normalScreenGeometry.normalGeometry.width; to: root.width
                duration: root.animationDuration; easing.type: Easing.InOutQuad
            }

            onStarted: {
                fullScreenLoader.active = true;
                normalScreenLoader.x = normalScreenGeometry.normalScreenX;
                normalScreenLoader.y = normalScreenGeometry.normalScreenY;
            }
            onFinished: {
                normalScreenLoader.active = false;
                mainWindow.enableWindowBlur(true);
            }
        }

        ParallelAnimation {
            id: exitFullScreenAnimation
            PropertyAnimation {
                target: backgroundMask; property: "x"
                from: 0; to: normalScreenGeometry.normalScreenX
                duration: root.animationDuration; easing.type: Easing.InOutQuad
            }
            PropertyAnimation {
                target: backgroundMask; property: "y"
                from: 0; to: normalScreenGeometry.normalScreenY
                duration: root.animationDuration; easing.type: Easing.InOutQuad
            }
            PropertyAnimation {
                target: backgroundMask; property: "height"
                from: root.height; to: normalScreenGeometry.normalGeometry.height
                duration: root.animationDuration; easing.type: Easing.InOutQuad
            }
            PropertyAnimation {
                target: backgroundMask; property: "width"
                from: root.width; to: normalScreenGeometry.normalGeometry.width
                duration: root.animationDuration; easing.type: Easing.InOutQuad
            }

            onStarted: {
                normalScreenLoader.active = true;
                mainWindow.enableWindowBlur(false);

            }
            onFinished: {
                normalScreenLoader.x = 0;
                normalScreenLoader.y = 0;
                backgroundMask.x = 0;
                backgroundMask.y = 0;
                fullScreenLoader.active = false;
                mainWindow.isFullScreen = false;
            }
        }
    }

    Loader {
        id: normalScreenLoader
        focus: !mainWindow.isFullScreen
        height: normalScreenGeometry.normalGeometry.height; width: normalScreenGeometry.normalGeometry.width
        sourceComponent: normalComponent
        NumberAnimation { id: normalShow; target: normalScreenLoader; properties: "opacity"; from: 0; to: 1; duration: root.animationDuration; easing.type: Easing.InQuint }
        NumberAnimation { id: normalHide; target: normalScreenLoader; properties: "opacity"; from: 1; to: 0; duration: root.animationDuration; easing.type: Easing.OutQuint }
        Component.onCompleted: {
            active = mainWindow.isFullScreen ? false : true;
            opacity = mainWindow.isFullScreen ? 0 : 1;
        }
        Keys.onPressed: {
            item.keyPressed(event);
        }
    }

    Loader {
        id: fullScreenLoader
        anchors.fill: parent
        sourceComponent: fullSceenComponent
        NumberAnimation { id: fullShow; target: fullScreenLoader; properties: "opacity"; from: 0; to: 1; duration: root.animationDuration; easing.type: Easing.InQuint }
        NumberAnimation { id: fullHide; target: fullScreenLoader; properties: "opacity"; from: 1; to: 0; duration: root.animationDuration; easing.type: Easing.OutQuint }

        Component.onCompleted: {
            active = mainWindow.isFullScreen ? true : false;
            opacity = mainWindow.isFullScreen ? 1 : 0;
        }
    }

    Component {
        id: fullSceenComponent
        AppUI.FullScreenUI {}
    }

    Component {
        id: normalComponent
        AppUI.NormalUI {}
    }
}
